﻿/*
 * 由SharpDevelop创建。
 * 用户： lucifer
 * 日期: 2011/3/17
 * 时间: 22:35
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;


namespace Yidu.Persister
{
	/// <summary>
	/// Description of DataHelper.
	/// </summary>
	public class DBHelper
	{
		private static DBHelper _dbHelper = null;
		private static object lockObject = new object();
		
		
		
		private DBHelper(){}
		
		public static string ConnectionString { get;set;}

		public static DBHelper Instance
		{
			get
			{
				if(_dbHelper == null){
					lock(lockObject){
						if(_dbHelper == null){
							_dbHelper = new DBHelper();
							if(string.IsNullOrEmpty(ConnectionString)){
								// ConnectionString = @"Data Source=YLUCIFER\SQLEXPRESS;Initial Catalog=GMS;User ID=sa;Password=orcl";
							    ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
							}
						}
					}
				}
				return _dbHelper;
			}
		}
		
		public SqlConnection CreateConnection()
		{
   
			return new SqlConnection(ConnectionString);
		}

		public DbProviderFactory DbProviderFactory{
			get{
				return null;
			}
		}

		public static void CloseQuiet(DbDataReader reader){
			if(reader == null){
				return;
			}
			try {
				reader.Close();
			} catch (Exception) {
			}
		}
		
		public static void CloseQuiet(DbConnection conn){
			if(conn == null){
				return;
			}
			try {
				conn.Close();
			} catch (Exception) {
			}
		}
		
		public static void CloseQuiet(DbDataReader reader,DbConnection conn){
			CloseQuiet(reader);
			CloseQuiet(conn);
		}
		
		public static void CancelQuiet(DbCommand command){
			if(command == null){
				return;
			}
			try {
				command.Cancel();
			} catch (Exception) {
			}
		}
		
		public static DbParameter createDbParameter(string name,object value){
			DbParameter param = new SqlParameter();
			param.ParameterName = name;
			param.Value = value;
			return param;
		}
	}
}
